var pageIndex = 1;
var sortArgs = "";
$(function(){
	hasNoPre();
	initNextButton();
});

function initNextButton(){
	var initSize = $("#hidNextInfoSize").val();
	if(initSize < 10){
		hasNoNext();
	}
}

function hasNoPre(){
	$("#divPre").empty();
	$("#divPre").html("<img src=\"image/btn_pre0.png\" style=\"cursor:pointer;\"/>");
}

function hasNoNext(){
	$("#divNext").empty();
	$("#divNext").html("<img src=\"image/btn_next0.png\" style=\"cursor:pointer;\"/>");
}

function hasNext(){
	$("#divNext").empty();
	$("#divNext").html("<img src=\"image/btn_next1.png\" style=\"cursor:pointer;\" onclick=\"nextPage();\"/>");
}
var sortType = " asc";
var clickNum = 0;
var eleTag;
function sort(field, ele){	
	if(eleTag != ele){
		$(eleTag).find("b").remove();
		$(eleTag).css("background-color", "#fff").css("color", "#666");
		eleTag = ele;
		sortType = "asc";
		clickNum = 0;
	}
	$(ele).find("b").remove();
	var text = $(ele).html();
	if(clickNum == 0){
		clickNum = clickNum + 1;
		sortType = " asc";
		//升序
		if(text.indexOf("b") < 0){
			//不包含图片
			text += "<b><img  style=\"height:10px;width:10px;margin-top:5px;\" src=\"image/asc.png\"/></b>";

			$(ele).html(text);
		}else{
			//包含图片
			$("a .a_sort> img").remove();
		}
		
	}else{
		clickNum = clickNum - 1;
		sortType = " desc";
		//降序
		if(text.indexOf("b") < 0){
			//不包含图片
			text += "<b><img src=\"image/desc.png\" style=\"height:10px;width:10px;margin-top:5px;\"/></b>";
			$(ele).html(text);
		}else{
			//包含图片
			$("a .a_sort> img").remove();
		}
	}
	
	$(ele).css("background-color", "#3CAC7E").css("color", "#fff");
	
	sortArgs = field + sortType;
	$.ajax({
	  	   type: "get",
	  	   dataType: "text",
	  	   url: ctx + "/rent.do?action=3&pageSize=10&pageIndex="+pageIndex + "&sortArgs="+sortArgs,
	  	   success: function(v){
	  		   if(v == ""){  			   
	  			   return;
	  		   }
	  		   
	  		   eval("var rentInfos="+v);
	  		   
	  		   
	  		   if(pageIndex == 1){
	  			   hasNoPre();
	  		   }
	  		   
	  		   hasNext();
	  		   $("#mainCenter").empty();

		  	   var htmlPreInfo = "";
	    	   $.each(rentInfos, function(index, value){
	    		   htmlPreInfo += getHomeSigleRentInfoHtml(value.rentId, value.rentPrice, value.rentType, value.vehicleType, value.rentTitle, value.imageId);
			   });
	    	   
	    	   $("#mainCenter").html(htmlPreInfo);
	  	   }
	     });
}
function getHomeSigleRentInfoHtml(rentId,rentPrice,rentType,vehicleType,rentTitle, imageId){
	var type = "个人租车";
	
	if(rentType == "1"){
		type = "商家租车";
	}
	
	var vehicleTypeJs = "z_icon.jpg";
	if(vehicleType == "0"){
		vehicleTypeJs = "d_icon.jpg";
	}
	
	var imgId = "nopic_100_100.jpg";
	if(imageId != "0"){
		imgId = imageId;
	}
	
	var str = "<div style=\"border:1px solid #dbe4eb;margin: 10px 0px 10px 0px;width:800px;float:left;height:100px;\">";
	str += "<div style=\"width:100px;height:100px;float:left;\">";
	str += "<img  style=\"width:100px;height:100px;\" src=\"vehicleImage/"+imgId+"\"/>";
	str += "</div>";
	str += "<div style=\"float:left;width:700px;height:100px;\">";
	str += "<div style=\"float:left:width: 700px;height:100px;\">";
	str += "<div style=\"width:390px;padding-left:10px;float:left;height:100px;\">";
	str += "<div style=\"color:#3b3b3b;width:390px;padding-left:10px;float:left;height:50px;font-size:14px;line-height:25px;vertical-align:middle;\">";
	str += ""+rentTitle+"";
	str += "</div>";
	str += "<div style=\"color:#3b3b3b;width:390px;padding-left:10px;float:left;height:25px;font-size:14px;vertical-align:middle;\">";
	str += "<img src=\"image/"+vehicleTypeJs+"\"/>";
	str += "</div>";
	str += "<div style=\"color:#444;width:390px;padding-left:10px;float:left;height:25px;font-size:14px;vertical-align:middle;\">";
	str += ""+type+"";
	str += "</div>";
	str += "</div>";
			
	str += "<div style=\"float:left;width:200px;height:100px;\"> ";
	str += "<div";
	str += " style=\"float: left; width: 50px; height: 100px; text-align: right; line-height: 90px; vertical-align: text-top; font-size: 20px; font-style: italic; color: #444;\">￥</div>";
	str += "<div";
		str += " style=\"float: left; width: 100px; height: 100px; text-align: center; line-height: 110px; font-size: 50px; color: #F60;\">";
	str += ""+rentPrice+"";
		str += "</div>";
			str += "<div style=\"float: left; width: 50px; height: 80px;padding-top:20px; text-align: left; line-height: 90px; vertical-align: bottom; font-size: 14px; font-style: italic; color: #444;\">";
	str += "				元/日";
	str += "			</div>";
	str += "		</div>";
			
	str += "		<div style=\"float:right;width:100px;height:80px;padding-top:20px;\">";

	str += "			<a class=\"detail-btn4\" href=\"rent.do?action=1&id="+rentId+"\" >租车</a>";
	str += "		</div>";
			
	str += "	</div>";
		
	str += "</div>";
	
	str += "</div>";
	return str;

}

function nextPage(){
	pageIndex = pageIndex + 1;
	$.ajax({
  	   type: "get",
  	   dataType: "text",
  	   url: ctx + "/rent.do?action=3&pageSize=10&pageIndex="+pageIndex + "&sortArgs="+sortArgs,
  	   success: function(v){
  		   if(v == ""){
  			   
  			   return;
  		   }
  		   
  		   eval("var rentInfos="+v);
  		   
	  	   if(rentInfos.length < 10){
	  		 hasNoNext();
  		   }
  		   
	  	   $("#mainCenter").empty();
	  	   var htmlNextInfo = "";
    	   $.each(rentInfos, function(index, value){
    		   htmlNextInfo += getHomeSigleRentInfoHtml(value.rentId, value.rentPrice, value.rentType, value.vehicleType, value.rentTitle, value.imageId);
		   });
    	   
    	   
    	   if(htmlNextInfo != ""){
    		   hasPre();
    	   }
    	   
    	   $("#mainCenter").html(htmlNextInfo);
  	   }
     });
}

function hasPre(){
	$("#divPre").empty();
	$("#divPre").html("<img src=\"image/btn_pre1.png\" style=\"cursor:pointer;\"  onclick=\"prePage();\"/>");
}

function prePage(){
	pageIndex = pageIndex -1;
	$.ajax({
  	   type: "get",
  	   dataType: "text",
  	   url: ctx + "/rent.do?action=3&pageSize=10&pageIndex="+pageIndex + "&sortArgs="+sortArgs,
  	   success: function(v){
  		   if(v == ""){  			   
  			   return;
  		   }
  		   
  		   eval("var rentInfos="+v);
  		   
  		   
  		   if(pageIndex == 1){
  			   hasNoPre();
  		   }
  		   
  		   hasNext();
  		   $("#mainCenter").empty();

	  	   var htmlPreInfo = "";
    	   $.each(rentInfos, function(index, value){
    		   htmlPreInfo += getHomeSigleRentInfoHtml(value.rentId, value.rentPrice, value.rentType, value.vehicleType, value.rentTitle, value.imageId);
		   });
    	   
    	   $("#mainCenter").html(htmlPreInfo);
  	   }
     });
}